﻿#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：ResBinData
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/6 21:57:56
*******************************************************************
* Copyright @ henrychang 2019. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using DataBase;
using System;
using System.IO;
using TouchFish.SingletonClass;
using UnityEngine;

namespace TouchFish.AssetLoad
{
    public class ResBinData : Singleton<ResBinData>, ISingleton
    {
        //public UI
        public void OnSingletonInit()
        {
        }

        #region 读取资源配置表

        private DataConfig_UIPath _dataConfigUIPath = null;

        public UIPathDefine GetUIPathById(uint id)
        {
            if (_dataConfigUIPath == null)
            {
                _dataConfigUIPath = new DataConfig_UIPath();
                DataConfig_UIPath.Deserialize(_dataConfigUIPath, GetDataReader("DataConfig_UIPath"));
            }

            return _dataConfigUIPath.GetUIPathByID(id);
        }

        #endregion 读取资源配置表

        #region 获取字节数组

        private tabtoy.DataReader GetDataReader(string dataConfigName)
        {
            byte[] bytesData = GetDataStream(dataConfigName);

            Stream stream = new MemoryStream(bytesData);

            stream.Position = 0;

            var reader = new tabtoy.DataReader(stream);

            if (reader.ReadHeader()) return reader;
            Console.WriteLine("combine file crack!");
            return null;
        }

        /// <summary>
        /// 根据文件名得到数据流
        /// </summary>
        private Byte[] GetDataStream(string fileName)
        {
            byte[] streamBytes = null;

            streamBytes = ReadFromResources(fileName);

            return streamBytes;
        }

        /// <summary>
        /// 从Resources中读取数据
        /// </summary>
        private byte[] ReadFromResources(string fileName)
        {
            byte[] streamBytes = LoadConfigFile("TabtoyDataBaseBin/" + fileName, "bytes");

            return streamBytes;
        }

        public byte[] LoadConfigFile(string resPath, string fileSuffix)
        {
            byte[] bytes = null;
            TextAsset ta = null;

            //if (BestGlobal.IsLoadAssetBundle)
            //{
            //    string fileUrl = string.Format("{0}{1}.{2}", BundleConfig.Instance.BundlesPathForPersist, resPath, fileSuffix);
            //    if (File.Exists(fileUrl))
            //    {
            //        bytes = File.ReadAllBytes(fileUrl);
            //    }
            //    else
            //    {
            //        ta = Resources.Load<TextAsset>(resPath);
            //        if (ta != null)
            //        {
            //            bytes = ta.bytes;
            //        }
            //    }
            //}
            //else
            //{
            //    ta = Resources.Load<TextAsset>(resPath);
            //    if (ta != null)
            //    {
            //        bytes = ta.bytes;
            //    }
            //}
            // TODO:目前配置表加载未考虑热更，后续补充
            ta = Resources.Load<TextAsset>(resPath);
            if (ta != null)
            {
                bytes = ta.bytes;
            }
            if (bytes == null)
            {
                Debug.LogError("can find skill config:" + resPath);
            }

            return bytes;
        }

        #endregion 获取字节数组
    }
}